/**
 * Created by 13521 on 2016/10/13.
 */
'use strict';

const mysql = require('koa-mysql');
const Promise = require('bluebird');
const pmysql = Promise.promisifyAll(require('mysql2'));
const redis = Promise.promisifyAll(require('redis'));

module.exports.init = function () {

    // console.dir(process.env);
    // redis cache
    var redisPassword = "";
    if (process.env.NODE_ENV == 'production') {
        redisPassword = (process.env.redispassword);
    } else {
        if (process.env.NODE_ENV == 'uat') {
            redisPassword = (process.env.redispassword);
        }
    }
    var redisConfig = {
        host: '127.0.0.1',
        port: 6379,
        socket_keepalive: true
    };
    console.dir(redisConfig);
    global.redisConfig = redisConfig;
    global.redisClient = redis.createClient(
        global.redisConfig
    );
    global.redisClient.on('error', function (err) {
        console.log('error event - ' + global.redisClient.host + ':'
            + global.redisClient.port + ' - ' + err);
    });

    // database
    let pool = null;
    let ppool = null;
    if (process.env.NODE_ENV == 'production') {
        console.log("process.env.NODE_ENV == 'production'");
        let password = (process.env.dbpassword);
        let mysql_config_prod = {
            connectionLimit: 50, //important
            acquireTimeout: 3000,
            host: 'localhost',
            user: 'root',
            password: password,
            database: 'sandbox',
            debug: false
        };
	      console.dir(mysql_config_prod);
        pool = mysql.createPool(mysql_config_prod);
        ppool = pmysql.createPool(mysql_config_prod);
    } else {
        if (process.env.NODE_ENV == 'uat') {
            console.log("process.env.NODE_ENV == 'uat'");
            let password = (process.env.dbpassword);
            let mysql_config_uat = {
                connectionLimit: 50, //important
                acquireTimeout: 3000,
                host: 'localhost',
                user: 'root',
                password: password,
                database: 'sandbox',
                debug: false
            };
	          console.dir(mysql_config_uat);
            pool = mysql.createPool(mysql_config_uat);
            ppool = pmysql.createPool(mysql_config_uat);
        } else {
            console.log("process.env.NODE_ENV == " + process.env.NODE_ENV);
            let mysql_config = {
                connectionLimit: 50, //important
                acquireTimeout: 3000,
                host: 'localhost',
                user: 'root',
                password: 'lt7116',
                database: 'todolist',
                debug: false
            };
	          console.dir(mysql_config);
            pool = mysql.createPool(mysql_config);
            ppool = pmysql.createPool(mysql_config);
        }
    }
    pool.on('enqueue', function () {
        console.warn('Waiting for available connection slot');
    });
    global.pool = pool;
    ppool = Promise.promisifyAll(ppool);
    ppool.on('enqueue', function () {
        console.warn('Waiting for available connection slot');
    });
    global.ppool = ppool;

    global.port = 80;
    if (global.argv && global.argv.p) {
        global.port = parseInt(global.argv.p);
    }

    global.wechatToken = "wechat";

    global.appid = process.env.wxappid || "wx8116d42dd40e0eb7";
    global.appsecret = process.env.wxappsecret || "9916149e8ba8ee34b8f4bf798183c6ef";

};
